package com.barneyx.mshinfoplatform.web.admin;

import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.info.InfoCateEntity;
import com.barneyx.mshinfoplatform.exception.MshException;
import com.barneyx.mshinfoplatform.util.InfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/admin/info/cate/")
@Slf4j
public class AdminInfoCateController {
    @Resource
    private InfoUtil.Cate cateUtil;



    @GetMapping("change-state")
    public ExResult<?> changeCateState(@RequestParam("id") String cateId,
                                       @RequestParam("type") String type,
                                       @RequestParam("state") boolean state) {

        return ExResult.ofAuto(cateUtil.ChangeCateShowType(cateId, type, state));
    }

    @GetMapping("update-sort")
    public ExResult<?> updateCateSort(@RequestParam("id") String cateId,
                                      @RequestParam(value = "sort", defaultValue = "0", required = false) int sort) {
        return ExResult.ofAuto(cateUtil.updateCateSort(cateId, sort));
    }

    @PostMapping("create")
    public ExResult<?> createCate(@RequestBody InfoCateEntity cateEntity) throws MshException {
        return ExResult.ofAuto(cateUtil.createCate(cateEntity), cateEntity);
    }

    @PutMapping("update")
    public ExResult<?> updateCate(@RequestBody InfoCateEntity cateEntity) throws MshException {
        return ExResult.ofAuto(cateUtil.updateCate(cateEntity), cateEntity);
    }


    @GetMapping("cate-get-by-id")
    public ExResult<?> getCateById(@RequestParam("id") String cateId) {
        return ExResult.ofAuto(cateUtil.getCateService().getById(cateId));
    }


    @DeleteMapping("delete")
    public ExResult<?> deleteCateById(@RequestParam("id") String cateId) {
        return ExResult.ofAuto(cateUtil.deleteById(cateId));
    }


}
